package com.xl98.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xl98.entity.*;
import com.xl98.service.ArticleService;
import com.xl98.service.TreeholeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/api/treehole")
@Api(value = "树洞",tags = {"树洞"})
public class TreeholeController {
    @Autowired
    private TreeholeService treeholeService;
    @Autowired
    private ArticleService articleService;


    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ApiOperation(value = "根据id、userId、articleId查询树洞，或者all=true查询全部")
    public Result findTreehole (@RequestBody Map<String, String> map) {
        List<Treehole> treeholeList = treeholeService.findTreehole(map);
        if(StringUtils.checkValNotNull(treeholeList)) {
            return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE, ResponseCode.SUCCESS_FIND, treeholeList);
        }
        else{
            return ResultFactory.buildFailResult(ResponseCode.FAIL_FIND);
        }
    }
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加内容到树洞里")
    public Result addTreehole (@RequestBody Map<String, Object> map) {
        Article article = JSON.parseObject(JSON.toJSONString(map.get("article")), Article.class);
        articleService.addArticle(article);
        Treehole treehole = new Treehole();
        treehole.setUserId((String)map.get("userId"));
        treehole.setUserNick((String)map.get("userNick"));
        treeholeService.addTreehole(treehole);
        HashMap<String, String> treeholeMap = new HashMap<>();
        treeholeMap.put("id",treehole.getId());
        treeholeMap.put("articleId",article.getId());
        treeholeService.insertArticleIdByupdate(treeholeMap);
        return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_INSERT);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新树洞内容")
    public Result updateTreehole (@RequestBody Map<String, Object> map) {
        Article article = JSON.parseObject(JSON.toJSONString(map.get("article")), Article.class);
        articleService.updateArticle(article);
        Treehole treehole = new Treehole();
        treehole.setUserNick((String)map.get("userNick"));
        UpdateWrapper<Treehole> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .ge("user_id",(String)map.get("userId"))
                .ge("article_id",article.getId());
        treeholeService.updateTreehole(treehole,updateWrapper);
        return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_UPDATE);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除树洞")
    public Result delete(String id){
        return ResultFactory.buildSuccessResult(treeholeService.delete(id));
    }
}

